/*-
 * Copyright (c) 2009-2012 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ Bitmap: validate_bitmap_v.c    Rev. 1.0.1    2012-05-06T11:41+08:00 $
 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include <bscerr.h>
#include <bitmap.h>


#define TEST_LENGTH_MOD 99


int generate_random(bitmap_t *ba);

int print_sector_bits(const bitmap_t *ba, size_t ssz);

void print_full_bits(uint8_t sctr);

void print_part_bits(uint8_t sctr, size_t size);


int main(int argc, char *argv[])
{
    int ierr = ERRNO_UNDEFINED;

#ifdef DEBUG_INFO
    size_t zi, zj;  /* size_t based loop counter or index */
#endif

    size_t tssz;  /* Test sample size */

    bitmap_t btar;
    bitmap_t btsy;

    size_t nmkz;
    size_t nnnz;


#ifdef DEBUG_INFO
    printf("Check Function: print_full_bits()");

    for (zi = 0; zi < 256; zi++)
    {
        printf("%3d ", (uint8_t) zi);
        print_full_bits((uint8_t) zi);
        printf("\n");
    }

    printf("Check Function: print_part_bits()");

    for (zi = 7; zi > 0; zi--)
    {
        for (zj = 0; zj < 256; zj++)
        {
            printf("%3d ", (uint8_t) zj);
            print_part_bits((uint8_t) zj, zi);
            printf("\n");
        }
    }
#endif

    srandom((unsigned long) time(NULL));

    tssz = (size_t) random() % TEST_LENGTH_MOD + 30;
    printf("Test Sample Size: %zd\n", tssz);

    ierr = bitmap_init(&btar, tssz);
    ierr = bitmap_init(&btsy, tssz);

    ierr = generate_random(&btar);

    ierr = print_sector_bits(&btar, 1);

    ierr = bitmap_get_marks(&btar, &nmkz);
    printf("Number of marks: %zd (By Old Method)\n", nmkz);

    ierr = bitmap_count_marks(&btar, &nmkz);
    printf("Number of marks: %zd (Test! By New Method)\n", nmkz);


    ierr = bitmap_get_nones(&btar, &nnnz);
    printf("Number of nones: %zd (By Old Method)\n", nnnz);

    ierr = bitmap_count_nones(&btar, &nnnz);
    printf("Number of nones: %zd (Test! By New Method)\n", nnnz);


    ierr = bitmap_dest(&btar);
    ierr = bitmap_dest(&btsy);

    return ierr;
}


int generate_random(bitmap_t *ba)
{
    int ierr = ERRNO_UNDEFINED;
    size_t zi;  /* size_t based loop counter or index */

#ifdef TRACE_INFO
    printf("[F] generate_random()\n");
#endif

    srandom((unsigned long) time(NULL));

    printf("Bit Array Random Data Setup Per Sector:\n");
    for (zi = 0; zi < ba->bksz; zi++)
    {
        ba->list[zi] = (uint8_t) (random() % 256);
        printf(" %.2X", ba->list[zi]);
    }
    printf("\n");

#ifdef TRACE_INFO
    printf("    generate_random() Passed!\n");
#endif

    return ierr;
}


int print_sector_bits(const bitmap_t *ba, size_t ssz)
{
    int ierr = ERRNO_UNDEFINED;
    size_t zi;  /* size_t based loop counter or index */

#ifdef TRACE_INFO
    printf("[F] print_sector_bits()\n");
#endif

    switch (ssz)
    {
        case 1:
            for (zi = 0; zi < ba->bksz - 1; zi++)
            {
                printf("[%5zd] ", zi);
                print_full_bits(ba->list[zi]);
                printf("\n");
            }

            printf("[%5zd] ", zi);
            print_part_bits(ba->list[ba->bksz - 1], 8 - (ba->bksz * 8 - ba->size));
            printf("\n");
            break;
        case 2:
            break;
        case 4:
            break;
        case 8:
            break;
        default:
            ierr = ERRNO_UNKNOWN_TYPE;
            break;
    }

#ifdef TRACE_INFO
    printf("    print_sector_bits() Passed!\n");
#endif

    return ierr;
}


void print_full_bits(uint8_t sctr)
{
    size_t zi;  /* size_t based loop counter or index */
    uint8_t quo;  /* Quotient */

    uint8_t dis[8];  /* Display form */

    quo = sctr;

    for (zi = 0; zi < 8; zi++)
    {
        if (quo % 2 == 0)
        {
            dis[7 - zi] = 0;
        }
        else
        {
            dis[7 - zi] = 1;
        }

        quo /= 2;
    }

    printf(" ");
    for (zi = 0; zi < 4; zi++)
    {
        printf("%d", dis[zi]);
    }

    printf(" ");
    for (zi = 4; zi < 8; zi++)
    {
        printf("%d", dis[zi]);
    }
}


void print_part_bits(uint8_t sctr, size_t size)
{
    size_t zi;  /* size_t based loop counter or index */
    uint8_t quo;  /* Quotient */

    uint8_t dis[8];  /* Display form */

    quo = sctr;

    for (zi = 0; zi < 8; zi++)
    {
        if (quo % 2 == 0)
        {
            dis[7 - zi] = 0;
        }
        else
        {
            dis[7 - zi] = 1;
        }

        quo /= 2;
    }

    printf(" ");
    for (zi = 0; zi < 4; zi++)
    {
        if (zi < size)
        {
            printf("%d", dis[zi]);
        }
        else
        {
            printf("-");
        }
    }

    printf(" ");
    for (zi = 4; zi < 8; zi++)
    {
        if (zi < size)
        {
            printf("%d", dis[zi]);
        }
        else
        {
            printf("-");
        }
    }
}

